package dataStructStack;

import java.util.ArrayList;
import java.util.Arrays;

public class MyStack {
    //  栈 的本身就是一个数组
    public int[] elem;
    public int usedSize;

    public MyStack(){
        this.elem = new int[10];
    }

    //  判断栈是否满了
    public boolean isFull(){
        return usedSize == elem.length;
    }

    //  push 压栈
    public void push(int val){
        if (isFull()){
            //  扩容
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize++] = val;
    }

    //  pop 出栈
    public int pop(){
        if(isNull()){
            throw new RuntimeException("栈为空 ！");
        }
        return elem[--usedSize];
    }
    // 判断栈是否为空
    public boolean isNull(){
        return usedSize == 0;
    }

    //  获取栈顶元素

    public int peek(){
        if(isNull()){
            throw new RuntimeException("栈为空 ！");
        }
        return elem[usedSize-1];
    }

}
